package com.example.administrator.radarplusfriendsdemo.custom;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.administrator.radarplusfriendsdemo.R;
import com.example.administrator.radarplusfriendsdemo.been.Info;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public class RadarPlusFriendView extends RelativeLayout {

    private Context mContext;

    @BindView(R.id.rv_radar)
    RadarView rvRadar;//雷达的View

    @BindView(R.id.rvg_container)
    RadarViewGroup rvgContainer;//雷达上头像的容器

    @BindView(R.id.btn_change)
    Button btnChange;//换一换

    @BindView(R.id.rg_state)
    RadioGroup rgState;//不限 男 女 的RadioGroup

    public RadarPlusFriendView(Context context) {
        this(context, null);
    }

    public RadarPlusFriendView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public RadarPlusFriendView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        View view = View.inflate(context, R.layout.radar_plus_friend_view_layout, this);
        ButterKnife.bind(this,view);
    }

    public void setDatas(@NonNull ArrayList<Info> mDatas) {
        rvgContainer.setDatas(mDatas);
    }

    @OnClick(R.id.btn_change)
    public void changePosition(View view){
        Toast.makeText(mContext, "换一换！", Toast.LENGTH_SHORT).show();
        rvgContainer.changePosition();
    }

    @OnCheckedChanged({R.id.rb_unlimited,R.id.rb_male,R.id.rb_female})
    public void OnCheckedChangeListener(CompoundButton view, boolean ischanged ){
        switch (view.getId()) {
            case R.id.rb_unlimited://不限
                if (ischanged){//注意：这里一定要有这个判断，只有对应该id的按钮被点击了，ischanged状态发生改变，才会执行下面的内容
                    if (mChooseStateListener != null){
                        mChooseStateListener.chooseState(TYPE_UN_LIMITED);
                    }
                }
                break;
            case R.id.rb_male://男性
                if (ischanged) {

                    if (mChooseStateListener != null){
                        mChooseStateListener.chooseState(TYPE_MALE);
                    }
                }
                break;
            case R.id.rb_female://女性
                if (ischanged) {
                    if (mChooseStateListener != null){
                        mChooseStateListener.chooseState(TYPE_FEMALE);
                    }
                }
                break;
        }
    }


    public static final int TYPE_UN_LIMITED = 1;//不限
    public static final int TYPE_MALE = 2;//男性
    public static final int TYPE_FEMALE = 3;//女性

    @IntDef({TYPE_UN_LIMITED, TYPE_MALE, TYPE_FEMALE})
    public @interface STATE{}//状态

    public interface ChooseStateListener{

        void chooseState(@STATE int state);
    }

    public ChooseStateListener mChooseStateListener;

    public RadarPlusFriendView setChooseStateListener(ChooseStateListener listener){
        mChooseStateListener = listener;
        return this;
    }
}
